﻿/******************************************************************************
 * Name         : LockedMap.hpp
 * Author       : 671643387
 * Date         : 2015年9月21日
 * Description  :
 *****************************************************************************/

#ifndef DEV_BASE_LOCKEDMAP_HPP_
#define DEV_BASE_LOCKEDMAP_HPP_

#include <boost/unordered_map.hpp>
#include <boost/noncopyable.hpp>
#include <dev/base/LockGuard.hpp>

namespace dev
{
    namespace base
    {
        template <
            typename T1, 
            typename T2, 
            typename LOCK, 
            typename STORAGE = boost::unordered_map<T1, T2>
        >
        class LockedMap : public boost::noncopyable
        {
        public:
            LockedMap()
                : storage_()
                , lock_()
            {

            }
            LockedMap(size_t size)
                : storage_(size)
                , lock_()
            {

            }
            ~LockedMap()
            {

            }

        public:
            T2& operator[](const T1& key)
            {
                LockGuard<LOCK> guard(lock_);
                return storage_[key];
            }

            void erase(const T1& key)
            {
                LockGuard<LOCK> guard(lock_);
                storage_.erase(key);
            }

            void clear(void)
            {
                LockGuard<LOCK> guard(lock_);
                storage_.clear();
            }

        private:
            STORAGE storage_;
            LOCK lock_;
        };
    }
}

#endif /* DEV_BASE_LOCKEDMAP_HPP_ */
